﻿using Microsoft.SharePoint;
using NGPT.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NGPT.ApproveIt
{
    /// <summary>
    /// Implement approval request form presenter.
    /// </summary>
    public class ApproveItPresenter
    {
        private const string DEFAULT_NON_TECHNICAL_ERROR_MESSAGE = "Unexpected error occured. See details in the ULS.";
        private const string RESULT_MESSAGE = "Your request has been sent to user {0} successfully.";

        IApproveItView view;
        IApprovalService service;

        /// <summary>
        /// Implement approval request form presenter.
        /// </summary>
        /// <param name="view">Request form view instance.</param>
        /// <param name="service">Approval service instance.</param>
        public ApproveItPresenter(IApproveItView view, IApprovalService service)
        {
            this.view = view;
            this.service = service;
        }

        /// <summary>
        /// Implement approval request UI command.
        /// </summary>
        public void RequestApproval()
        {
            view.ErrorMessage = string.Empty;
            view.ResultMessage = string.Empty;

            try
            {
                service.RequestApproval(view.ApproverLoginName, view.RequestText, view.SiteUrl, view.ListId, view.ItemId);
                view.ResultMessage = string.Format(RESULT_MESSAGE, view.ApproverLoginName);
            }
            catch (ArgumentException ex)
            {
                view.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                view.ErrorMessage = DEFAULT_NON_TECHNICAL_ERROR_MESSAGE;
                Logger.LogError(ex);
            }
        }
    }
}
